import pymysql
class DButils:

    def __init__(self, host, user, password, database, port=3306, charset='utf8'):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
        self.charset = charset

    def update(self, sql,data=None):
        db = pymysql.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            database=self.database,
            port=self.port,
            charset=self.charset
        )
        try:
            with db.cursor() as cursor:
                cursor.execute(sql,data)
                db.commit()
                print('数据操作成功！')
        except Exception as e:
            print('数据操作失败！',e)
        finally:
            db.close()

    def select(self, sql,data=None):
        db = pymysql.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            database=self.database,
            port=self.port,
            charset=self.charset
        )
        try:
            with db.cursor() as cursor:
                cursor.execute(sql,data)
                result = cursor.fetchall()
                return result
        except Exception as e:
            print('数据查询失败！',e)
        finally:
            db.close()

if __name__ == '__main__':
    db = DButils('localhost', 'root', '20021224', 'person_massege')
    person_list=[('张三',18,'男',1.78),('李四',20,'男',1.77),('赵武',19,'男',1.80),('李倩',21,'女',1.60),('王娜娜',19,'女',1.65),('孙三',21,'男',1.79),('马尼',18,'女',1.66),('赵二',23,'男',1.85),('丽丽',19,'女',1.62),('刘大',22,'男',1.77)]
    sql = "insert into person values(%s,%s,%s,%s)"
    for i in person_list:
        db.update(sql,i)